﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Util의 요약 설명입니다.
/// </summary>

namespace TimeCapsule
{
	public class Util
	{
		public Util()
		{
			//
			// TODO: 여기에 생성자 논리를 추가합니다.
			//
		}

		public static string DateFormatChange(string Date, string mode)
		{
			//Date
			//	012345678901234567890123
			//	2011-10-04 오후 3:30:34
			//	2011-10-05 오전 11:01:15
			switch (mode)
			{
				case "yyyy-mm-dd":
					return Date.Substring(0, 9);
				case "yy-mm-dd":
					return Date.Substring(2, 8);
				case "yymmdd":
					return Date.Substring(2, 2) + Date.Substring(5, 2) + Date.Substring(8, 2);
				case "yy.mm.dd":
					return Date.Substring(2,2) + "." + Date.Substring(5,2) + "." + Date.Substring(8,2);
				
				default:
					return Date;
			}
		}
		
		public static string generateEncrytedKey()
		{
			string format = "{1}{3}{4}{2}{0}";
			
			Random rnd = new Random();
			int i1 = rnd.Next(100,999);
			int i2 = rnd.Next(1000,9999);
			int i3 = rnd.Next(100,999);
			int n = i1 % 3 + i2 % 2 + i3 % 5;
			
			return string.Format(format, i1, i2, i3, n, DateTime.Now.ToFileTime() );
		}

		public static bool decryptKey(string enc, out string date)
		{
			string[] res = new string[5];

			res[0] = enc.Substring(26, 3);
			res[1] = enc.Substring(0, 4);
			res[2] = enc.Substring(23, 3);
			res[3] = enc.Substring(4, 1);
			res[4] = enc.Substring(5, 18);
			
			if( int.Parse(res[3]) != ( int.Parse(res[0]) % 3 + int.Parse(res[1]) % 2 + int.Parse(res[2]) % 5 ) )
			{
				date = "";
				return false;
			}

			date = DateTime.FromFileTime(long.Parse(res[4])).ToString();
			return true;
		}
		
		public static bool decryptKey(string enc)
		{
			string[] res = new string[5];

			res[0] = enc.Substring(26, 3);
			res[1] = enc.Substring(0, 4);
			res[2] = enc.Substring(23, 3);
			res[3] = enc.Substring(4, 1);
			res[4] = enc.Substring(5, 18);
			DateTime date = DateTime.FromFileTime(long.Parse(res[4]));
			
			if (int.Parse(res[3]) != (int.Parse(res[0]) % 3 + int.Parse(res[1]) % 2 + int.Parse(res[2]) % 5))
			{
				return false;
			}
			
			if( DateTime.Compare( date, DateTime.Now ) < 0 && DateTime.Compare( date, DateTime.Now.AddDays(-3) ) > 0 )
				return true;
			
			return false;
		}
		
		public static string generateKey(int length)
		{
			string key ="";
			string str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
			char[] chars = str.ToCharArray();
			
			System.Random rand = new System.Random();
			
			for (int i=0; i<length; i++)
			{
				key += chars[rand.Next(0, 62)];
			}
			
			return key;
		}
		
		public static void CheckError()
		{
			if( ErrorMessage.IsError() )
				HttpContext.Current.Server.Transfer("~/Error.aspx");
		}
	}
}
